﻿using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace TCPServerRedirectedOfADB
{

    public class Server
    {  
        // Incoming data from the client.
        public string recievedData = null;

        public string sentData = null;

        public Boolean connectionState;

        
        public Server (int portnumber)
        {
            StartListening(portnumber);
            connectionState = true;
        }               


        private Boolean StartListening(int portno)
        {
            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Disconnect
            Boolean connectionState=true;

            // Establish the local endpoint for the socket.
            // Dns.GetHostName returns the name of the 
            // host running the application.
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, portno);

            // Create a TCP/IP socket.
            Socket serverSocket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and 
            // listen for incoming connections.
            try
            {
                serverSocket.Bind(localEndPoint);
                serverSocket.Listen(10);

                // Start listening for connections.
                while (connectionState)
                {
                    //    Console.WriteLine("Waiting for a connection...");
                    // Program is suspended while waiting for an incoming connection.
                    Socket handler = serverSocket.Accept();
                    recievedData = null;

                    // An incoming connection needs to be processed.
                    while (true)
                    {
                        bytes = new byte[1024];
                        int bytesRec = handler.Receive(bytes);
                        recievedData += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                        if (recievedData.IndexOf("<EOF>") > -1)
                        {
                            break;
                        }
                    }

                    // Show the data on the console.
                    // Console.WriteLine("Text received : {0}", data);

                    if (sentData != null)
                    {
                        // Echo the data back to the client.
                        byte[] msg = Encoding.ASCII.GetBytes(sentData);
                        handler.Send(msg);
                        sentData = null;
                    }

                    if (!connectionState)
                    {
                        handler.Shutdown(SocketShutdown.Both);
                        handler.Close();
                    }
                }

            }
            catch (Exception e)
            {
                //  Console.WriteLine(e.ToString());

            }

         //   Console.WriteLine("\nPress ENTER to continue...");
         //   Console.Read();

            return connectionState;

        }

    }
}
